home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / UUDECODE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-30  |  2.2 KB  |  81 lines

  1. /* This is a junky piece of C designed to take a uuencoded file
  2.    as the first parameter and decode it into the file given as
  3.    the second parameter. there is no error checking or other
  4.    warning if it encounters a problem. The purpose of this
  5.    program is to act as a simple bootstrap to enable the initial
  6.    transfer of a file to allow more advanced binary transfers.
  7.  
  8.    In particular, note that the first line of the file to be
  9.    decoded should be the 'begin' line produced by a uuencode.
  10.  
  11.    This code (for what it is worth) is Copyright 1990, by
  12.    David R. Evans, NQ0I.
  13.  
  14. */
  15.  
  16. #define byte unsigned char
  17.  
  18. #include <stdio.h>
  19. #ifndef _lint
  20. #include <stdlib.h>
  21. #endif
  22. #include <string.h>
  23.  
  24. #if !defined(_lint)
  25. static char rcsid[] OPTIONAL = "$Id: uudecode.c,v 1.8 1997/07/31 00:44:20 root Exp root $";
  26. #endif
  27.  
  28. int
  29. main(int argc, char *argv[])
  30. {
  31. char line[100];
  32. FILE *infile, *outfile;
  33. long linecount = 1;
  34.   
  35.     if (argc != 3)    {
  36.         printf ("Usage: uudecode fromfile tofile\n\a");
  37.         exit (1);
  38.     }
  39.     
  40.     /* open the files */
  41.     infile = fopen(argv[1], "rt");
  42.     outfile = fopen(argv[2], "wb");
  43.  
  44.     /* skip the first (begin) line */
  45.     (void) fgets(line, 100, infile);
  46.  
  47.     /* decode the remainder of the file */
  48.     while ((void)fgets(line, 100, infile), strncmp(line, "end", 3))    {
  49.         int n_to_write, n_to_read, n_reads, n, m;
  50.         byte b[4], out[3];
  51.  
  52.         if (!(++linecount % 10))
  53.             printf ("Processing line #%ld...\n", linecount);
  54.         n_to_write = (int)line[0] - 32;
  55.         n_to_read = ((n_to_write + 2) / 3) * 4;
  56.         if(strlen((char *)line) - 2 < (unsigned int) n_to_read)    /* EOF apparently */
  57.             n_to_read = 0;
  58.         n_reads = n_to_read / 4;
  59.         for (n = 0; n < n_reads; n++)    {
  60.             for (m = 0; m < 4; m++)
  61.                 b[m] = (unsigned char) line[1 + 4 * n + m] - 32;
  62.  
  63.             /* now do the actual decode */
  64.             out[0] = (unsigned char) (b[0] << 2) | ((b[1] & 0x30) >> 4);
  65.             out[1] = ((b[1] & 0x0f) << 4) | ((b[2] & 0x3c) >> 2);
  66.             out[2] = ((b[2] & 0x03) << 6) | (b[3]);
  67.             if(n_to_write > 3)
  68.                 for (m = 0; m < 3; m++)
  69.                     fputc(out[m], outfile);
  70.             else
  71.                 for (m = 0; m < n_to_write; m++)
  72.                     fputc(out[m], outfile);
  73.             n_to_write -= m;
  74.         }
  75.     }
  76.     (void) fclose (outfile);
  77.     (void) fclose (infile);
  78.     return 0;
  79. }
  80.  
  81.